package servlet;

import dao.UsersDao;
import vo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by ssw on 2018/6/12.
 */
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //         设置编码
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String uid=req.getParameter("uid");
        String upass=req.getParameter("upass");
        UsersDao dao=new UsersDao();
        User user=dao.login(uid,upass);
        if(user==null){
//      TODO 提示用户名、密码输入错误，让用户重新输入
            resp.sendRedirect("index.jsp?msg=10");
        }
        else{
            HttpSession session=req.getSession();
            session.setAttribute("user",user);
            if(user.getUid().startsWith("admin"))
                resp.sendRedirect("sgms/adminUI.jsp");
            if(user.getUid().startsWith("lec"))
                resp.sendRedirect("sgms/lecUI.jsp");
            if(user.getUid().startsWith("user"))
                resp.sendRedirect("sgms/userUI.jsp");
        }
    }
}
